package com.googlecode.wmlynar.gwtbeans.example.client;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.googlecode.wmlynar.gwtbeans.client.beans.BindingGroup;
import com.googlecode.wmlynar.gwtbeans.client.beans.SourcesPropertyChangeEvents;
import com.googlecode.wmlynar.gwtbeans.client.ui.TextBox;

public class Application implements EntryPoint, SourcesPropertyChangeEvents {

    private final PropertyChangeSupport support = new PropertyChangeSupport(this);

    private ExampleModel model;
    private TextBox textBox1;
    private TextBox textBox2;

    public void onModuleLoad() {
        final RootPanel rootPanel = RootPanel.get();
        final VerticalPanel verticalPanel = new VerticalPanel();
        rootPanel.add(verticalPanel);

        final HTML html = new HTML(
                "<h2>This example demonstrates binding between two text boxes and the model</h2>");
        verticalPanel.add(html);

        textBox1 = new TextBox();
        verticalPanel.add(textBox1);

        textBox2 = new TextBox();
        verticalPanel.add(textBox2);

        model = new ExampleModel();
        model.setText("example text");

        final BindingGroup binding = BindingFactory.createBinding(this, model, null);
        binding.bind();
        binding.copyBToA();
    }

    public void addPropertyChangeListener(final String arg0, final PropertyChangeListener arg1) {
        support.addPropertyChangeListener(arg0, arg1);
    }

    public void removePropertyChangeListener(final String arg0, final PropertyChangeListener arg1) {
        support.removePropertyChangeListener(arg0, arg1);
    }

    public void setTextBox1(final TextBox textBox1) {
        support.firePropertyChange("textBox1", this.textBox1, this.textBox1 = textBox1);
    }

    public TextBox getTextBox1() {
        return textBox1;
    }

    public void setTextBox2(final TextBox textBox2) {
        support.firePropertyChange("textBox2", this.textBox2, this.textBox2 = textBox2);
    }

    public TextBox getTextBox2() {
        return textBox2;
    }

}
